﻿using _8Group.MES.ISongService.ProWorkorder;
using _8Group.MES.ISongService.ProWorkorder.DTO;
using _8Group.MES.ISongService.WmBarCode;
using _8Group.MES.Model.PRO生产管理;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _8Group.MES.SongService
{
    [ApiExplorerSettings(GroupName = "宋恒漳")]
    public class ProWorkorderService : ApplicationService, IProWorkorderService
    {
        private readonly IRepository<Pro_Workorder, int> _workorderRepository;
        public ProWorkorderService(IRepository<Pro_Workorder, int> workorderRepository)
        {
            _workorderRepository = workorderRepository;
        }
        /// <summary>
        /// 获取所有工单
        /// </summary>
        /// <returns></returns>
        public async Task<object> GetProWorkorderAsync(RequestProWorkorderDTO workorderDTO)
        {
            var wmItemRecptList = await _workorderRepository.GetQueryableAsync();
            wmItemRecptList = wmItemRecptList.WhereIf(!string.IsNullOrEmpty(workorderDTO.WorkorderCode), x => x.WorkorderCode.Contains(workorderDTO.WorkorderCode));
            wmItemRecptList = wmItemRecptList.WhereIf(!string.IsNullOrEmpty(workorderDTO.WorkorderName), x => x.WorkorderName.Contains(workorderDTO.WorkorderName));
            var count = wmItemRecptList.Count();
            wmItemRecptList = wmItemRecptList.OrderBy(x => x.Id).Skip(((workorderDTO.Page - 1) ?? 0) * (workorderDTO.Limit ?? 10)).Take(workorderDTO.Limit ?? 10);
            var result = new ResponseModel
            {
                code = "",
                count = count,
                data = wmItemRecptList.ToList(),
                msg = "",
            };
            return result;
        }

        /// <summary>
        /// 删除工单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteProWorkorderAsync(int id)
        {
            var workorder = await _workorderRepository.GetAsync(id);
            await _workorderRepository.DeleteAsync(workorder, true);
            return true;
        }

        /// <summary>
        /// 添加工单
        /// </summary>
        /// <param name="workorderDTO"></param>
        /// <returns></returns>
        public async Task<bool> AddProWorkorderAsync(AddProWorkorderDTO workorderDTO)
        {
            //判断名称是否重复
            var workorder = await _workorderRepository.FirstOrDefaultAsync(x => x.WorkorderName == workorderDTO.WorkorderName);
            if (workorder != null)
            {
                return false;
            }
            workorder = AutoMapperExt.MapTo<Pro_Workorder>(workorderDTO);
            workorder.Create_By = "宋恒漳";
            workorder.Creste_Time = DateTime.Now;            
            await _workorderRepository.InsertAsync(workorder);
            return true;

        }

        /// <summary>
        /// 更新工单
        /// </summary>
        /// <param name="workorderDTO"></param>
        /// <returns></returns>
        public async Task<bool> UpdateProWorkorderAsync(UpdateProWorkorderDTO workorderDTO)
        {
            workorderDTO.Updateby = "宋恒漳";
            workorderDTO.UpDateTime = DateTime.Now;
            var workorder = AutoMapperExt.MapTo<Pro_Workorder>(workorderDTO);
            await _workorderRepository.UpdateAsync(workorder);
            return true;
        }
    }
}
